﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class CustomerPOItemController:ControllerBase
    {


        public void addCustomerPoItem(CustomerPOItemEntity customerPoItemEntity)
        {
            CustPOItem custPoItem = TransformCustomerPOItemEntityToCustPOItem(customerPoItemEntity);
            Sdc.CustPOItems.InsertOnSubmit(custPoItem);
            Sdc.SubmitChanges();
        }
        public static CustPOItem TransformCustomerPOItemEntityToCustPOItem(CustomerPOItemEntity customerPoItemEntity)
        {
            CustPOItem custPoItem=new CustPOItem();
            custPoItem.CompleteQty = customerPoItemEntity.CompleteQty;
            custPoItem.CustPOId = customerPoItemEntity.CustPOId;
            custPoItem.Lot = customerPoItemEntity.Lot;
            custPoItem.Price = customerPoItemEntity.Price;
            custPoItem.ProductId = customerPoItemEntity.ProductId;
            custPoItem.Remark = customerPoItemEntity.Remark;
            custPoItem.RequestDate = customerPoItemEntity.RequestDate;
            custPoItem.RequestQty = customerPoItemEntity.RequestQty;
            custPoItem.Status = customerPoItemEntity.Status;
            return custPoItem;
        }



        public IList<CustomerPOItemEntity> findCustomerPOItemsByCustPOId(int custPOId)
        {
            IList<CustPOItem> custPoItems = (from c in Sdc.CustPOItems
                                     where c.CustPOId==custPOId
                                     select c).ToList();
            IList<CustomerPOItemEntity> customerPoItemEntities = new List<CustomerPOItemEntity>();
            foreach (var custPoItem in custPoItems)
            {
                CustomerPOItemEntity customerPoItemEntity = TransformCustPOItemToCustomerPOItemEntity(custPoItem);
                customerPoItemEntities.Add(customerPoItemEntity);
            }
            return customerPoItemEntities;
        }

        public IList<CustomerPOItemEntity> findUncompletedCustomerPOItemsByCustPOId(int customerPoId)
        {
            IList<CustPOItem> custPoItems = (from c in Sdc.CustPOItems
                                             where c.CustPOId == customerPoId && c.Status != "completed"
                                             select c).ToList();
            IList<CustomerPOItemEntity> customerPoItemEntities = new List<CustomerPOItemEntity>();
            foreach (var custPoItem in custPoItems)
            {
                CustomerPOItemEntity customerPoItemEntity = TransformCustPOItemToCustomerPOItemEntity(custPoItem);
                customerPoItemEntities.Add(customerPoItemEntity);
            }
            return customerPoItemEntities;
        }


        public CustomerPOItemEntity findCustomerPOItemByCustomerPOItemId(int poItemId)
        {
            CustPOItem custPoItem = (from c in Sdc.CustPOItems
                                     where c.Id == poItemId
                                     select c).SingleOrDefault();
            CustomerPOItemEntity customerPoItemEntity = TransformCustPOItemToCustomerPOItemEntity(custPoItem);
            return customerPoItemEntity;
        }

        public static CustomerPOItemEntity TransformCustPOItemToCustomerPOItemEntity(CustPOItem custPoItem)
        {
            CustomerPOItemEntity customerPoItemEntity = new CustomerPOItemEntity();
            customerPoItemEntity.CompleteQty = custPoItem.CompleteQty;
            customerPoItemEntity.CustPOId = custPoItem.CustPOId;
            customerPoItemEntity.CustPONo = custPoItem.CustPO.No;
            customerPoItemEntity.Lot = custPoItem.Lot;
            customerPoItemEntity.Price = custPoItem.Price;
            customerPoItemEntity.ProductId = custPoItem.ProductId;
            customerPoItemEntity.Remark = custPoItem.Remark;
            customerPoItemEntity.RequestDate = custPoItem.RequestDate;
            customerPoItemEntity.RequestQty = custPoItem.RequestQty;
            customerPoItemEntity.OpenQty = custPoItem.RequestQty - custPoItem.CompleteQty;
            customerPoItemEntity.Status = custPoItem.Status;
            customerPoItemEntity.ProductName = custPoItem.Product.Name;
            customerPoItemEntity.ProductNo = custPoItem.Product.No;
            customerPoItemEntity.FormatDate = customerPoItemEntity.RequestDate.ToString("d MMM, yyyy");
            customerPoItemEntity.ProductNameAndRequestedDate = customerPoItemEntity.ProductName + ", " +
                                                               customerPoItemEntity.FormatDate;
            customerPoItemEntity.ProductNoAndRevision = custPoItem.Product.No + "/ " + custPoItem.Product.Revision;
            customerPoItemEntity.Amount = customerPoItemEntity.Price*customerPoItemEntity.RequestQty;
            customerPoItemEntity.Id = custPoItem.Id;
            
            customerPoItemEntity.Revision = custPoItem.Product.Revision;
            customerPoItemEntity.RequestDate = custPoItem.RequestDate;

            
            
            return customerPoItemEntity;
        }



        public int updateCustPOItem(int custPoItemId, int qty)
        {
            CustPOItem custPoItem = (from c in Sdc.CustPOItems
                                     where c.Id == custPoItemId
                                     select c).SingleOrDefault();
            custPoItem.CompleteQty = custPoItem.CompleteQty + qty;
            Sdc.SubmitChanges();
            return custPoItem.CompleteQty;
        }
        public void updateCustPOItem(int custPoItemId, string status)
        {
            CustPOItem custPoItem = (from c in Sdc.CustPOItems
                                     where c.Id == custPoItemId
                                     select c).SingleOrDefault();
            custPoItem.Status = status;
            Sdc.SubmitChanges();
        }

        public void DeleteCustomerPOByCustomerPOId(int custPOId)
        {
            IList<CustPOItem> custPoItems = (from c in Sdc.CustPOItems
                                             where c.CustPOId == custPOId
                                             select c).ToList();
            foreach (CustPOItem custPoItem in custPoItems)
            {
                Sdc.CustPOItems.DeleteOnSubmit(custPoItem);
                Sdc.SubmitChanges();
            }
        }

        public void UpdateCustomerPOItemByCustomerPOItemId(CustomerPOItemEntity CustomerPoItemEntity)
        {
            CustPOItem CustPOItem = (from v in Sdc.CustPOItems
                                     where v.Id == CustomerPoItemEntity.Id
                                     select v).SingleOrDefault();
            CustPOItem.ProductId = CustomerPoItemEntity.ProductId;
            CustPOItem.RequestDate = CustomerPoItemEntity.RequestDate;
            CustPOItem.Price = CustomerPoItemEntity.Price;
            CustPOItem.Lot = CustomerPoItemEntity.Lot;
            CustPOItem.RequestQty = CustomerPoItemEntity.RequestQty;
            CustPOItem.Status = CustomerPoItemEntity.Status;
            CustPOItem.Remark = CustomerPoItemEntity.Remark;
            Sdc.SubmitChanges();

        }

        public void DeleteCustomerPOItem(int customerPOItemId, ref string msg)
        {
            try
            {
                CustPOItem CustPOItem = (from v in Sdc.CustPOItems
                                         where v.Id == customerPOItemId
                                         select v).SingleOrDefault();
                Sdc.CustPOItems.DeleteOnSubmit(CustPOItem);
                Sdc.SubmitChanges();
            }
            catch(Exception e)
            {
                msg = e.Message;
            }
        }


        public IList<CustomerPOItemEntity> FindContractPODueCustomerPOItems(int lots)
        {
            IList<CustPOItem> custPoItems = (from pi in Sdc.CustPOItems
                                   where pi.CustPO.Type == "contract" && (pi.RequestQty - pi.CompleteQty) <= pi.Lot*lots && pi.Status!="completed"
                                   select pi).ToList();
            IList<CustomerPOItemEntity> customerPoItemEntities = new List<CustomerPOItemEntity>();
            foreach (var custPoItem in custPoItems)
            {
                CustomerPOItemEntity customerPoItemEntity = TransformCustPOItemToCustomerPOItemEntity(custPoItem);
                customerPoItemEntities.Add(customerPoItemEntity);
            }
            return customerPoItemEntities;
        }

        public string FindItemCategoryByItemNo(string gvNo)
        {
            try
            {
                string itemCategory = (from c in Sdc.Products
                                       where c.No == gvNo
                                       select c.Category.Name).FirstOrDefault();
                return itemCategory;
                
            }
            catch (Exception e)
            {
                return null;
            }
        }

        
    }
}
